package com.imooc.dao;

import java.io.IOException;
import java.util.List;

import javax.websocket.Session;

import org.apache.ibatis.session.SqlSession;

import com.imooc.beans.Message;
import com.imooc.db.Database;

public class MessageDao {
	
	public List<Message> queryMessageList(String command,String description){
		
		Database database= new Database();
		SqlSession session=null;
		List<Message> mList=null;
		try {
			Message message= new Message();
			message.setCommand(command);
			message.setDescription(description);
			
			 session = database.getSession();
			 mList = session.selectList("Message.queryMessageList",message);
			
			session.commit();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(session!=null)
				session.close();
				
		}
		
		
	
		return mList;
	}
	
	/**
	 * @param id
	 */
	public void deleteOne(int id){
		
		Database database= new Database();
		SqlSession session=null;
		
		try {
				
			 session = database.getSession();
			  session.delete("Message.deleteOne",id);
			
			session.commit();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(session!=null)
				session.close();
				
		}
	}
	
public void deleteBatch(List<Integer> ids){
		
		Database database= new Database();
		SqlSession session=null;
		
		try {
				
			 session = database.getSession();
			  session.delete("Message.deleteBatch",ids);
			
			session.commit();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(session!=null)
				session.close();
				
		}
	}
public void addMessage(Message message){
	
	Database database= new Database();
	SqlSession session=null;
	
	try {
			
		 session = database.getSession();
		  session.insert("Message.addMessage",message);
		
		session.commit();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally {
		if(session!=null)
			session.close();
			
	}
}


}
